package controller;

import domain.Teacher;
import service.TeacherService;

import java.util.Scanner;

public class TeacherController {

    TeacherService teacherService = new TeacherService();
    Scanner sc = new Scanner(System.in);

    public void start() {
        while (true) {
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    //System.out.println("添加老师");
                    add();
                    break;
                case "2":
                    //System.out.println("删除老师");
                    delete();
                    break;
                case "3":
                    //System.out.println("修改老师");
                    update();
                    break;
                case "4":
                    //System.out.println("查看老师");
                    findAll();
                    break;
                case "5":
                    System.out.println("退出");
                    return;
                default:
                    System.out.println("您的输入有误，请重新输入");
                    break;
            }
        }
    }

    public void update() {
        String updeId;
        while (true) {
            System.out.println("请输入学号");
            updeId = sc.next();
            boolean exists = teacherService.isExists(updeId);
            if (!exists) {
                System.out.println("查无信息，请重新输入");
            } else {
                break;
            }
        }
        System.out.println("请输入姓名");
        String name = sc.next();
        System.out.println("请输入年龄");
        String age = sc.next();
        System.out.println("请输入生日");
        String birthday = sc.next();

        Teacher newT = new Teacher();
        newT.setId(updeId);
        newT.setName(name);
        newT.setAge(age);
        newT.setBrithday(birthday);
        teacherService.update(updeId, newT);
        System.out.println("修改成功");
    }

    public void delete() {
        String delId;
        while (true) {
            System.out.println("请输入学号");
            delId = sc.next();
            boolean exists = teacherService.isExists(delId);
            if (!exists) {
                System.out.println("查无信息，请重新输入");
            } else {
                break;
            }
        }
        teacherService.delete(delId);
        System.out.println("删除成功");
    }

    public void findAll() {
        Teacher[] teachers = teacherService.findAll();
        if (teachers == null) {
            System.out.println("查无信息，请重新输入");
            return;
        }
        System.out.println("学号\t姓名\t年龄\t生日");
        for (int i = 0; i < teachers.length; i++) {
            Teacher teacher = teachers[i];
            if (teacher != null) {
                System.out.println(teacher.getId() + "\t" + teacher.getName() + "\t" + teacher.getAge() + "\t\t" + teacher.getBrithday());
            }
        }
    }

    public void add() {
        //键盘录入信息
        //循环录入学号，直到学号没有重复
        String id;
        while (true) {
            System.out.println("请输入学号");
            id = sc.next();
            //将录入的ID进行判断，若存在则重新录入
            boolean exists = teacherService.isExists(id);
            if (exists) {
                System.out.println("学号已存在，请重新输入");
            } else {
                break;
            }
        }

        System.out.println("请输入姓名");
        String name = sc.next();
        System.out.println("请输入年龄");
        String age = sc.next();
        System.out.println("请输入生日");
        String birthday = sc.next();
        //将录入的信息进行封装为老师对象，并给封装的老师对象赋值
        Teacher t = new Teacher();
        t.setId(id);
        t.setName(name);
        t.setAge(age);
        t.setBrithday(birthday);

        //调用add方法若返回true，则添加成功，若返回false则添加失败
        boolean flag = teacherService.add(t);
        if (flag) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }
}
